package com.ahua.mallchat.common.domain.enums.system;

import cn.hutool.http.ContentType;
import com.ahua.mallchat.common.domain.vo.ApiResult;
import com.ahua.mallchat.common.utils.JsonUtils;
import com.google.common.base.Charsets;
import lombok.AllArgsConstructor;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@AllArgsConstructor
public enum HttpErrorEnum {
    ACCESS_DENIED(401, "登录失效，请重新登录");
    private Integer httpCode;
    private String desc;

    public Integer getErrorCode() {
        return httpCode;
    }

    public String getErrorDesc() {
        return desc;
    }

    public void sendHttpError(HttpServletResponse response) throws IOException {
        response.setContentType(ContentType.JSON.toString(Charsets.UTF_8));
        response.setStatus(this.getErrorCode());
        ApiResult responseData = ApiResult.fail(getErrorCode(), getErrorDesc());
        response.getWriter().write(JsonUtils.toStr(responseData));
    }
}
